<?php

namespace App\models\forum;

use App\models\user\User;
use App\other\Notification\Mention;
use Auth;
use Illuminate\Database\Eloquent\Model;
use Parsedown;
use Purifier;

/**
 * App\models\forum\Replie
 *
 * @property int $id
 * @property string $source
 * @property int $topic_id
 * @property int $uid
 * @property string $is_blocked
 * @property int $vote_count
 * @property string $body
 * @property string $body_original
 * @property string $deleted_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read \App\models\user\User $user
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereSource($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereTopicId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereUid($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereIsBlocked($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereVoteCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereBody($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereBodyOriginal($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int $index
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\Replie whereIndex($value)
 * @property-read \App\models\forum\Topic $topic
 */
class Replie extends Model{
    public function user(){
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function topic(){
        return $this->belongsTo(Topic::class, 'topic_id', 'id');
    }

    public function createReplie($data){
        if ($this->isDuplicateReply($data)){
            return false;
        }
        $parsedown              = new Parsedown();
        $mention                = new Mention();
        $content                = $parsedown->text($mention->parse($data['body']));
        $body                   = Purifier::clean($content, 'user_topic_body');
        $this->uid              = Auth::id();
        $this->is_blocked       = 'no';
        $this->vote_count       = 0;
        $this->body             = $body;
        $this->body_original    = $data['body'];
        return true;
    }

    public function isDuplicateReply($data){
        $last_reply = self::where('uid', Auth::id())
            ->where('topic_id', $data['topic_id'])
            ->orderBy('id', 'desc')
            ->first();
        return count($last_reply) && strcmp($last_reply->body_original, $data['body']) === 0;
    }
}
